package com.ssps.back.manage.permission.service.impl;

import com.ssps.back.manage.permission.MyUtils;
import com.ssps.back.manage.permission.service.Manager_PermissionService;
import com.ssps.entity.Permission;
import com.ssps.entity.Role;
import com.ssps.entity.RolePermissionExample;
import com.ssps.entity.RolePermissionKey;
import com.ssps.mapper.PermissionMapper;
import com.ssps.mapper.RoleMapper;
import com.ssps.mapper.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class Manager_PermissionServiceImpl implements Manager_PermissionService {
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<RolePermissionKey> getByRid(Integer rid) {
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRidEqualTo(rid);
        return rolePermissionMapper.selectByExample(example);
    }

    @Override
    public void assign(Integer rid, String pids) {
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRidEqualTo(rid);
        rolePermissionMapper.deleteByExample(example);
        String[] split = pids.substring(0, pids.length() - 1).split(",");
        for (String pid : split) {
            if (!"".equals(pid) && pid != null) {
                RolePermissionKey rolePermission = new RolePermissionKey();
                rolePermission.setRid(rid);
                rolePermission.setPmid(Integer.parseInt(pid));
                rolePermissionMapper.insertSelective(rolePermission);
            }
        }
    }

    @Override
    public List<Permission> list() {
        List<Permission> nodes = permissionMapper.selectByExample(null);
        for (Permission node : nodes) {
           node.setName( node.getPname());
        }
        return MyUtils.getTree(nodes);
    }

    @Override
    public void add(Integer pid, String name) {
        Permission permission = new Permission();
        System.out.println(pid);
        permission.setParentid(pid);
        permission.setPname(name);;
        permissionMapper.insertSelective(permission);
    }

    @Override
    public void update(Integer pid, String name) {
        Permission permission = new Permission();
        permission.setPmid(pid);
        permission.setPname(name);
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public List<Role> selectAllRoles() {
        List<Role> roles = roleMapper.selectByExample(null);
        return roles;
    }

    @Override
    public void delete(Integer pid) {
        permissionMapper.deleteByPrimaryKey(pid);
    }
}
